﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace Erebos
{
    enum MultiTexture2DType
    {
        SidesAndCorners,
        OnlySides
    }

    class MultiTexture2D
    {
        private Texture2D _leftTopCorner;
        private Texture2D _topSide;
        private Texture2D _rightTopCorner;
        private Texture2D _rightSide;
        private Texture2D _rightButtomCorner;
        private Texture2D _buttomSide;
        private Texture2D _leftButtomCorner;
        private Texture2D _leftSide;
        private Texture2D _fill;
        private MultiTexture2DType _type;
        private bool _enableFill;

        public MultiTexture2D(Texture2D topSide, Texture2D rightSide, Texture2D buttomSide, Texture2D leftSide)
        {
            _topSide = topSide;
            _rightSide = rightSide;
            _buttomSide = buttomSide;
            _leftSide = leftSide;
            _type = MultiTexture2DType.OnlySides;
        }

        public MultiTexture2D(Texture2D leftTopCorner, Texture2D topSide, Texture2D rightTopCorner, Texture2D rightSide, 
            Texture2D rightButtomCorner, Texture2D buttomSide, Texture2D leftButtomCorner, Texture2D leftSide)
        {
            _leftTopCorner = leftTopCorner;
            _topSide = topSide;
            _rightTopCorner = rightTopCorner;
            _rightSide = rightSide;
            _rightButtomCorner = rightButtomCorner;
            _buttomSide = buttomSide;
            _leftButtomCorner = leftButtomCorner;
            _leftSide = leftSide;
            _type = MultiTexture2DType.SidesAndCorners;
        }

        public void EnableFill(Texture2D fill)
        {
            _fill = fill;
            _enableFill = true;
        }

        public void Draw(SpriteBatch spriteBatch, Rectangle destinationRectangle, Color color)
        {
            int widthWithoutCorners = destinationRectangle.Width - (_leftTopCorner.Width + _rightTopCorner.Width);
            int heightWithoutCorners = destinationRectangle.Height - (_leftTopCorner.Height + _leftButtomCorner.Height);
            spriteBatch.Draw(_leftTopCorner, new Vector2(destinationRectangle.X, destinationRectangle.Y), color);
            spriteBatch.Draw(_topSide, new Rectangle(destinationRectangle.X + _leftTopCorner.Width, destinationRectangle.Y, widthWithoutCorners, _leftTopCorner.Height), color);
            spriteBatch.Draw(_rightTopCorner, new Vector2(destinationRectangle.X + destinationRectangle.Width - _rightTopCorner.Width, destinationRectangle.Y), color);
            spriteBatch.Draw(_rightSide, new Rectangle(destinationRectangle.X + destinationRectangle.Width - _rightTopCorner.Width, destinationRectangle.Y + _rightTopCorner.Width, _rightSide.Width, heightWithoutCorners), color);
            spriteBatch.Draw(_rightButtomCorner, new Vector2(destinationRectangle.X + destinationRectangle.Width - _rightTopCorner.Width, destinationRectangle.Y + _rightTopCorner.Width + heightWithoutCorners), color);
            spriteBatch.Draw(_buttomSide, new Rectangle(destinationRectangle.X + _leftTopCorner.Width, destinationRectangle.Y + _rightTopCorner.Width + heightWithoutCorners, widthWithoutCorners, _leftTopCorner.Height), color);
            spriteBatch.Draw(_leftButtomCorner, new Vector2(destinationRectangle.X, destinationRectangle.Y + _rightTopCorner.Width + heightWithoutCorners), color);
            spriteBatch.Draw(_leftSide, new Rectangle(destinationRectangle.X, destinationRectangle.Y + _rightTopCorner.Width, _rightSide.Width, heightWithoutCorners), color);
            if (_enableFill)
                spriteBatch.Draw(_fill, new Rectangle(destinationRectangle.X + _leftTopCorner.Width, destinationRectangle.Y + _leftTopCorner.Height, widthWithoutCorners, heightWithoutCorners), color);
        }
    }
}
